package com.ubing.boot.notesforjava.test.juc;

public class FixedSizeList<T> {
    private Object[] elements;
    private int size;

    public FixedSizeList() {
        elements = new Object[1000];
        size = 0;
    }

    public synchronized void add(T element) {
        if (size < 1000) {
            elements[size] = element;
            size++;
        } else {
            throw new IllegalStateException("List size is already 1000");
        }
    }

    public synchronized T get(int index) {
        if (index >= size) {
            throw new IndexOutOfBoundsException("Index is out of bounds");
        }
        return (T) elements[index];
    }

    public synchronized void remove(int index) {
        if (index >= size) {
            throw new IndexOutOfBoundsException("Index is out of bounds");
        }

        for (int i = index; i < size - 1; i++) {
            elements[i] = elements[i + 1];
        }

        size--;
        elements[size] = null;
    }

    public synchronized int size() {
        return size;
    }
}
